package com.atwulidun.queue;

import java.util.Scanner;


public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3);
        char key;
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'e':
                    scanner.close();
                    System.out.print("程序退出，谢谢使用!");
                    loop = false;
                    break;
                case 'a':
                    System.out.println("请输入要填加到队列的数据：");
                    int data = scanner.nextInt();
                    queue.addQueue(data);
                    break;
                case 'g':
                    try {
                        int res = queue.getQueue();
                        System.out.println("取出的数据为：" + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = queue.headQueue();
                        System.out.println("头部数据为：" + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
            }
        }
    }
}

class ArrayQueue {
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        front = -1;// 指向队列第一个元素的前一位
        rear = -1;// 指向队列最后一个元素
        arr = new int[maxSize];
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        return rear == (maxSize - 1);
    }

    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列已满，不能添加！");
            return;
        }
        arr[++rear] = n;
    }

    // 获取队列的头数据，此时front的值增加1
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能获取数据！");
        }
        front++;
        int temp = arr[front];// 取出元素后要把该位置置0
        arr[front] = 0;
        return temp;
    }

    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空，不能显示数据！");
            return;
        }
        for (int i = front + 1; i < rear + 1; i++) {
            System.out.printf("arr[%d] = %d\n", i, arr[i]);
        }
    }

    // 查看队列的头数据，此时front的值不变
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能获取数据");
        }
        return arr[front + 1];// 注意：这里是front+1，而不是++front
    }
}
//public class ArrayQueueDemo {
//    public static void main(String[] args) {
//        ArrayQueue arrayQueue = new ArrayQueue(3);
//        Scanner scanner = new Scanner(System.in);
//        boolean flag = true;
//        char key;
//        while (flag) {
//            System.out.println("add");
//            System.out.println("get");
//            System.out.println("head");
//            System.out.println("show");
//            System.out.println("exit");
//            key = scanner.next().charAt(0);
//            switch (key) {
//                case 'a':
//                    System.out.println("please enter a number");
//                    int num = scanner.nextInt();
//                    arrayQueue.addQueue(num);
//                    break;
//                case 'h':
//                    try {
//                        int num1 = arrayQueue.headQueue();
//                        System.out.println("the top number is" + num1);
//                    } catch (RuntimeException e) {
//                        System.out.println(e.getMessage());
//                    }
//                    break;
//                case 'g':
//                    try {
//                        int num1 = arrayQueue.getQueue();
//                        System.out.println("the top number is" + num1);
//                    } catch (RuntimeException e) {
//                        System.out.println(e.getMessage());
//                    }
//                    break;
//                case 's':
//                    try {
//                        arrayQueue.showQueue();
//                    } catch (RuntimeException e) {
//                        System.out.println(e.getMessage());
//                    }
//                    break;
//                case 'e':
//                    scanner.close();
//                    System.out.println("thanks for your using!");
//                    flag = false;
//                    break;
//            }
//        }
//    }
//}
//class ArrayQueue{
//    private int[] queue;
//    private int maxsize;
//    private int front;
//    private int rear;
//
//    public ArrayQueue(int maxsize) {
//        this.maxsize = maxsize;
//        queue = new int[maxsize];
//        front = -1;
//        rear = -1;
//    }
//
//    public boolean isEmpty() {
//        return front == rear;
//    }
//
//    public boolean isFull() {
//        return rear == (maxsize - 1);
//    }
//
//    public void addQueue(int num) {
//        if (isFull()) {
//            System.out.println("the queue is full");
//            return;
//        }
//        queue[++rear] = num;
//    }
//
//    public int getQueue() {
//        if (isEmpty()) {
//            throw new RuntimeException("the queue is empty");
//        }
//        int temp = queue[++front];
//        queue[front] = 0;
//        return temp;
//    }
//
//    public int headQueue() {
//        if (isEmpty()) {
//            throw new RuntimeException("the queue is empty");
//        }
//        return queue[front + 1];
//    }
//
//    public void showQueue() {
//        if (isEmpty()) {
//            throw new RuntimeException("the queue is empty");
//        }
//        for (int i = front + 1; i <= rear; i++) {
//            System.out.printf("arr[%d] = %d\n", i, queue[i]);
//        }
//    }
//}
